const fs = require('fs')
const path = require('path')
const fsP = require("fs/promises")

// 异步读取文件
fs.readFile(path.resolve(__dirname, './test.txt'), 'utf8', (err, data) => {
    if (err) {
        console.error(err);
        return;
    }
    console.log(data);
});

// 同步读取文件
try {
    const data = fs.readFileSync(path.resolve(__dirname, './test.txt'), 'utf8')
    console.log(data);
} catch (err) {
    console.error(err);
}

// Promise的方式读取文件
fsP.readFile(path.resolve(__dirname, './test.txt'), 'utf8').then(data => {
    console.log(data);
})

// 流式读取文件
const stream = fs.createReadStream(path.resolve(__dirname, './maxfile.txt'), 'utf8')
stream.on('data', (chunk) => {
    console.log(chunk);
})
stream.on('end', () => {
    console.log('>>>>读取完成<<<<');
})